package com.nes.redismq.springboot.redis;

import com.nes.redismq.springboot.redis.OneMessageListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;

/**
 * Created by wdq on 17-3-14.
 */
@Configuration
public class RedisConfig {

    @Bean
    public StringRedisTemplate template(RedisConnectionFactory connectionFactory){
        return new StringRedisTemplate(connectionFactory);
    }

//    @Bean
//    public RedisConnectionFactory connectionFactory(){
//        JedisConnectionFactory factory = new JedisConnectionFactory();
//        factory.setPassword("123456");
//        factory.setDatabase(1);
//        return factory;
//    }

    //注册监听器
    @Bean
    public RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory){
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        container.addMessageListener(new OneMessageListener(), new PatternTopic("chat"));

        return container;
    }


}
